package com.hyl.controller;

import com.hyl.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

/**
 * BaseController
 *
 * @author hyl
 * @date 2025-07-02
 */
public class BaseController {

    //转发
    public String forward(String url){
        return UrlBasedViewResolver.FORWARD_URL_PREFIX + url;
    }
    //重定向
    public String redirect(String url){
        return UrlBasedViewResolver.REDIRECT_URL_PREFIX + url;
    }
    //request
    public HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    //request添加数据
    public void setRequestAttribute(String key, Object value){
        getRequest().setAttribute(key, value);
    }
    //session
    public HttpSession getSession(){
        return getRequest().getSession();
    }
    //session添加数据
    public void setSessionAttribute(String key, Object value){
        getSession().setAttribute(key, value);
    }
    //session获取数据
    public Object getSessionAttribute(String key){
        return getSession().getAttribute(key);
    }

    public void setCurrentUser(SysUser user){
        setSessionAttribute("loginUser", user);
    }

    public SysUser getCurrentUser(){
        return (SysUser) getSessionAttribute("loginUser");
    }

    public Integer getCurrentUserID(){
        return getCurrentUser().getId();
    }
}
